package cn.byapps.attendance.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import cn.byapps.attendance.entity.Clazz;
import cn.byapps.attendance.entity.Dept;
import cn.byapps.attendance.entity.Dorm;
import cn.byapps.attendance.entity.Student;
import cn.byapps.attendance.service.StudentService;
import cn.byapps.attendance.util.DataTablesResult;
import cn.byapps.attendance.util.Pager;
import core.controller.BaseController;
import core.excel.ExportExcel;
import core.excel.ImportExcel;

@Controller
@RequestMapping("/std")
public class StudentController extends BaseController {

	@Resource
	StudentService studentService;

	@RequestMapping(value = { "index", "/", "" })
	public String index() {
		return "person/index";
	}

	@RequestMapping(value = { "stdindex" })
	public String studentIndex(HttpServletRequest request, Model model) {
		Object object = request.getSession().getAttribute("loginUser");
		if (object != null) {
			Student student = (Student) object;
			student = studentService.getById(student.getId());
			model.addAttribute("item", student.getFavJob());
		}
		return "person/stdIndex";
	}

	@Deprecated
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(String curPage, String pageCount, Model model) {
		Pager<Student> pager = studentService.getPage(curPage, pageCount);
		model.addAttribute("pager", pager);
		return "person/list";
	}

	@RequestMapping(value = { "getData" }, method = RequestMethod.POST)
	public void getData(String clazz,String dept,Integer draw, Long start, Long length, HttpServletResponse response) {
		int pos = 0;
		int rows = 10;
		if (start != null) {
			pos = start.intValue();
		}
		if (length != null) {
			rows = length.intValue();
		}
		DataTablesResult<Student> lists = studentService.getData(draw, pos, rows,dept,clazz);
		if (lists == null) {
			lists = new DataTablesResult<Student>();
		}
		try {
			this.writeJSON(response, lists);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Deprecated
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add() {
		return "person/add";
	}

	@RequestMapping(value = "down", method = RequestMethod.GET)
	public String down() {
		return "person/downFile";
	}

	@Deprecated
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String add(Student person) {
		studentService.addPerson(person);
		return "redirect:list";
	}

	@RequestMapping(value = "update", method = RequestMethod.GET)
	public String update(String id, Model model) {
		Student student = studentService.getById(id);
		model.addAttribute("person", student);
		return "person/update";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(Student person) {
		studentService.updatePerson(person);
		return "redirect:index";
	}

	@RequestMapping(value = "delete", method = RequestMethod.GET)
	public String delete(String id) {
		studentService.deleteById(id);
		return "redirect:index";
	}

	@RequestMapping(value = "getclazzes", method = RequestMethod.GET)
	public void getClazzes(HttpServletResponse response) {
		List<Clazz> jobs = studentService.findClazzes();
		if (jobs == null) {
			jobs = new ArrayList<Clazz>();
		}
		try {
			this.writeJSON(response, jobs);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "getdorms", method = RequestMethod.GET)
	public void getDorms(HttpServletResponse response) {
		List<Dorm> jobs = studentService.findDorms();
		if (jobs == null) {
			jobs = new ArrayList<Dorm>();
		}
		try {
			this.writeJSON(response, jobs);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "getdepts", method = RequestMethod.GET)
	public void getDept(HttpServletResponse response) {
		List<Dept> jobs = studentService.findDepts();
		if (jobs == null) {
			jobs = new ArrayList<Dept>();
		}
		try {
			this.writeJSON(response, jobs);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 导出一个数据模版
	@RequestMapping(value = "exportTemp", method = RequestMethod.GET)
	public void exportMod(HttpServletResponse response) throws IOException {
		List<Student> records = new ArrayList<Student>();
		Student s = new Student("1691694430", "王小明");
		records.add(s);
		String fileName;
		// if (b) {
		fileName = "studentTemplate.xls";
		// } else {
		// fileName = "studentTemplate.xlsx";
		// }
		new ExportExcel(null, Student.class, true).setDataList(records).write(response, fileName).dispose();
	}

	@RequestMapping(value = "import", method = RequestMethod.GET)
	public String upFile() {
		return "person/importData";
	}

	@RequestMapping(value = "import", method = RequestMethod.POST)
	public String upFile(MultipartFile file, String dept, String clazz, HttpServletRequest request, Model model)
			throws IOException {
		if (!file.isEmpty()) {
			ImportExcel imp = null;
			try {
				imp = new ImportExcel(file, 0, 0);
				List<Student> stus = imp.getDataList(Student.class, null);
				for (Student s : stus) {
					s.setClazz(clazz);
					s.setDept(dept);
					studentService.addPerson(s);
				}
			} catch (InvalidFormatException e) {
				e.printStackTrace();
			} catch (InstantiationException | IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return "redirect:index";
	}
}
